package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;
import cm.mefobe.android.telecomcost.adapter.TimePeriodRadioAdapter;
import cm.mefobe.android.telecomcost.dialog.TimePeriodPickerDialog;
import cm.mefobe.android.telecomcost.entities.TimePeriod;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;

public class TimePeriodRadioListView extends ListView {

	private Context _context;
	private ArrayList<TimePeriod> periode_list;
	private TimePeriodRadioAdapter _adapter;
	private TimePeriod selectedPeriod;
	private TimePeriodPickerDialog _dialog;

	public void setDialog(TimePeriodPickerDialog dialog) {
		_dialog = dialog;
	}

	public TimePeriodRadioListView(Context context) {
		super(context);
		_context = context;
	}

	public TimePeriodRadioListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
	}

	public TimePeriodRadioListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
	}

	private void setList() {
		Log.i("TimePeriodRadioListView",
				"setting the TimePeriodRadioListView list...");
		if (_context != null) {
			periode_list = new ArrayList<TimePeriod>();
			periode_list.add(TimePeriod.SECOND);
			periode_list.add(TimePeriod.MINUTE);
			periode_list.add(TimePeriod.HOUR);
			periode_list.add(TimePeriod.DAY);
			periode_list.add(TimePeriod.WEEK);
			periode_list.add(TimePeriod.MONTH);
			periode_list.add(TimePeriod.YEAR);

			_adapter = new TimePeriodRadioAdapter(_context,
					android.R.layout.select_dialog_singlechoice, periode_list);
		} else {
			Log.i("TimePeriodRadioListView",
					"cannot set the adapter of the TimePeriodRadioListView...the context is null");
		}
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		Log.i("TimePeriodRadioListView", "setting the adapter...");
		try {
			setList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.setAdapter(_adapter);
	}

	@Override
	public void setSelection(int position) {
		Log.i("TimePeriodRadioListView", "setSelection..." + position);
		selectedPeriod = _adapter.getItem(position);
		if (_dialog != null) {
			_dialog.setPeriode(selectedPeriod);
		} else {
			Log.i("TimePeriodRadioListView", "dialog not set");
		}
		// super.setSelection(position);
	}

	public TimePeriod getSelectedPeriod() {
		return selectedPeriod;
	}
}
